<?php

//文章标签库
class TagLibJob extends TagLib {
	
	//分页栏html代码
	private $_pager;
	
	// 初始化标签库的定义文件
	public function _initialize() {
		
		$this->xml = dirname ( __FILE__ ) . '/Tags/Job.xml';
	
	}
	
	public function _list($attr, $content) {
		$tag = $this->parseXmlAttr ( $attr, 'include' );
		
		$size = empty ( $tag ['size'] ) ? 10 : ( int ) $tag ['size'];
		
		$column = empty ( $tag ['col'] ) ? 1 : ( int ) $tag ['col'];
		
		$strlen = $tag ['strlen'];
		
		$li = $tag ['li'];
		
		$showDate = empty ( $tag ['show_date'] ) ? true : $tag ['show_date'];
		
		$dateFormat = empty ( $tag ['date_format'] ) ? 'Y-m-d' : $tag ['date_format'];
		
		$order = empty ( $tag ['order'] ) ? 'id desc' : $tag ['order'];
		if (empty ( $order )) {
			$order = "id desc";
		}
		
		$target = empty ( $tag ['target'] ) ? '_blank' : $tag ['target'];
		
		//是否显示分页
		$pager = $tag ['pager'];
		//分页大小
		$page_size = empty ( $tag ['page_size'] ) ? 20 : ( int ) $tag ['page_size'];
		
		$dao = D ( 'Job' );
		//where条件
		$w = ' checked=1';
		//分页
		if ($pager) {
			import ( "ORG.Util.Page" );
			$count = $dao->where ( $w )
				->count ( 'id' );
			$pg = new Page ( $count, $page_size );
			//得到分页的列表
			$list = $dao->where ( $w )
				->order ( 'top desc,`sort` asc,' . $order )
				->limit ( $pg->firstRow . ',' . $pg->listRows )
				->findAll ();
			$this->_pager = $pg->show ();
		} else {
			//得到$size条
			$list = $dao->where ( $w )
				->order ( 'top desc,`sort` asc,' . $order )
				->limit ( $size )
				->findAll ();
		}
		
		$count = count ( $list );
		for($i = 0; $i < $count; $i ++) {
			$r = & $list [$i];
			$id = $r ['id'];
			
			//链接
			$r ['url'] = __APP__ . "/Job/show/id/" . $id;
			
			//截取标题长度
			$r ['old_title'] = $r ['job_name'];
			if (! empty ( $strlen ))
				$r ['title'] = msubstr ( $r ['job_name'], 0, $strlen ); //substr($r['title'],0,$strlen);
			else
				$r ['title'] = $r ['job_name'];
			
		//日期格式
			$r ['date'] = dateFormat ( $r ['pub_date'], $dateFormat );
		
		}
		
		$tagTpl = ( int ) $tag ['tpl']; //自定义标签模板
		// 1.表格形式的列表
		if (empty ( $tagTpl )) { //
			
			$s = '<table width="100%"><tr>';
			for($i = 0; $i < count ( $list ); $i ++) {
				$r = $list [$i];
				
				$s .= "<td>";
				if (! empty ( $li )) {
					if (is_numeric ( $li ))
						$li_img = C ( 'LI_IMG_DIR' ) . $li . '.gif';
					else
						$li_img = C ( 'LI_IMG_DIR' ) . $li;
					
					$s .= "<img src=\"{$li_img}\" align=\"absmiddle\" />&nbsp;";
				}
				$s .= "<a href=\"{$r['url']}\" target=\"{$target}\" title=\"{$r['old_title']}\">{$r['title']}</a></td>";
				
				//是否显示日期
				if ($showDate) {
					$date = $r ['date'];
					$s .= "<td>{$date}</td>";
				}
				
				if ((($i + 1) % $column) == 0)
					$s .= '</tr><tr>';
			
			}
			$s .= '</tr></table>';
			if( $pager )
			{
				$s .= '<table><tr><td>'.$this->_pager.'</td></tr></table>';
			}
			return $s;
		} 
		else //2. 可自定义标签模板的调用
		{
			$file = C ( 'TPL_PATH' ) . $tagTpl . '.html';
			$view = Think::instance ( 'View' );
			
			$view->assign ( 'list', $list );		
			$view->assign ( 'target', $target );
			$view->assign ( 'strlen', $strlen );
			if ($pager) //输出分页栏
				$view->assign ( 'pager', $this->_pager );
			
			return $view->fetch ( $file );
		
		}
	
	}
}
?>
    